import org.codehaus.groovy.runtime.InvokerHelper;

import org.codehaus.groovy.grails.plugins.autodelegator.Delegator

class AutoDelegatorGrailsPlugin {
    def version = 0.1.1
    def dependsOn = [:]

    // TODO Fill in these fields
    def author = "Franz Allan Valencia See"
    def authorEmail = ""
    def title = "This plugin automatically does delegation"
    def description = '''\
This plugin adds a 'delegatee' attribute to the service classes which
would allow the invoation of the delegatee's public methods from the
containing service class instance. 
'''

    // URL to the plugin's documentation
    def documentation = "http://code.google.com/p/grails-auto-delegator-plugin/"

        static {
        // Check whether the JVM supports annotations.
        try {
            // Try to load the Annotation class dynamically.
            Class.forName('java.lang.annotation.Annotation')

            // The Annotation class was loaded fine, so we can check
            // for the TypeArg annotation. We load the annotation
            // class dynamically so that the plugin can be used with
            // the 1.4 JDK.
            Class.forName('org.codehaus.groovy.grails.plugins.autodelegator.annotation.Delegate')
        }
        catch (ClassNotFoundException ex) {
        }
    }

    def doWithSpring = {
    }
   
    def doWithApplicationContext = { applicationContext ->
    }

    def doWithWebDescriptor = { xml ->
    }
    
    def doWithDynamicMethods = { applicationContext  ->
        application.allClasses.each { aClass ->
            aClass.metaClass.invokeMethod = { String name, args -> return Delegator.delegatedInvokeMethod(delegate, name, args); }
        }
    }
	
    def onChange = { event ->
    }

    def onConfigChange = { event ->
    }
    
}
